﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DAL;
using CommontLibrary;

public partial class UserControl_HocVien_ChuyenContact : System.Web.UI.UserControl
{
#region "Member variables"
    private int intUserID = 0;
    bool boolRead = false;
    bool boolWrite = false;
    bool boolUpdate = false;
    bool boolDelete = false;
    DANguoidung objNguoidung;
    DAHocVien objHocVien;
    DAQuyen objQuyen;
    #endregion
#region "Parametter variables"
    // Ham lay Bind du lieu
    public SqlConnection ConnectionView
    {
        get
        {
            object obj2 = this.ViewState["ConnectionView"];
            if (obj2 != null)
            {
                return (SqlConnection)obj2;
            }
            return null;
        }
        set
        {
            this.ViewState["ConnectionView"] = value;
        }
    }
    #endregion
#region "Private methods"
    private void loadQuyenNguoiDung(int QuyenID,int intUserID,ComponentArt.Web.UI.TreeView tvTemp,bool checkView)
    {
        tvTemp.Nodes.Clear();
        DataSet dsNguoiDungQuyen = objNguoidung.loadQuyenNguoiDungByQuyen(QuyenID,intUserID);
        if (dsNguoiDungQuyen != null && dsNguoiDungQuyen.Tables[0].Rows.Count > 0)
        {
            int i = 1;
            foreach (DataRow dr in dsNguoiDungQuyen.Tables[0].Rows)
            {
                int intNguoiID = Convert.ToInt32(dr["ID"].ToString());
                int intCount = objHocVien.loadDemSoHocVienTheoNguoiDung(intNguoiID, 0);
                ComponentArt.Web.UI.TreeViewNode rootNode = new ComponentArt.Web.UI.TreeViewNode();
                rootNode.Text = dr["HoTen"].ToString() + " (" + intCount.ToString() + ")";
                rootNode.ImageUrl = "root.gif";
                rootNode.ID = intNguoiID.ToString();
                rootNode.Value = dr["ID"].ToString();
                rootNode.ShowCheckBox = checkView;
                tvTemp.Nodes.Add(rootNode);
                i++;
                if (checkView)
                {
                    DataSet dsLevel = objNguoidung.loadNguoiDungLevel(intNguoiID);
                    foreach (DataRow drLevel in dsLevel.Tables[0].Rows)
                    {
                        int intCountLevel = objHocVien.loadDemSoHocVienTheoNguoiDung(intNguoiID, Convert.ToInt32(drLevel["ID"].ToString()));
                        ComponentArt.Web.UI.TreeViewNode rootNodeLevel = new ComponentArt.Web.UI.TreeViewNode();
                        rootNodeLevel.Text = drLevel["Ten"].ToString() + " (" + intCountLevel.ToString() + ")";
                        rootNodeLevel.ImageUrl = "root.gif";
                        rootNodeLevel.ID = drLevel["ID"].ToString();
                        rootNodeLevel.Value = drLevel["ID"].ToString();
                        rootNodeLevel.ShowCheckBox = checkView;
                        rootNode.Nodes.Add(rootNodeLevel);
                        DataSet dsHocVienDanhsach = objHocVien.loadDanhSachHocVienTheoNguoiDung(intNguoiID, Convert.ToInt32(drLevel["ID"].ToString()));
                        if (dsHocVienDanhsach != null && dsHocVienDanhsach.Tables[0].Rows.Count > 0)
                        {
                            BuildTreeSub(dsHocVienDanhsach.Tables[0].Rows, rootNodeLevel, checkView);
                        }
                    }
                }
            }
        }
    }
    private void BuildTreeSub(DataRowCollection dsHocVienDanhsach, ComponentArt.Web.UI.TreeViewNode parentNode, bool checkView)
    {
        foreach (DataRow dr in dsHocVienDanhsach)
        {
            int LevelID = Convert.ToInt32(dr["ID"].ToString());
            ComponentArt.Web.UI.TreeViewNode node = new ComponentArt.Web.UI.TreeViewNode();
            node.Text = dr["HoTen"].ToString() ;
            node.ID = dr["ID"].ToString();
            node.Value = dr["ID"].ToString();
            node.ShowCheckBox = checkView;
            parentNode.Nodes.Add(node);
        }
    }   
    private void loadNhomGoc(DropDownList ddlTemp)
    {
        DataSet dsQuyen = objQuyen.loadQuyen();
        if (dsQuyen != null && dsQuyen.Tables[0].Rows.Count > 0)
        {
            DataRow[] drs = dsQuyen.Tables[0].Select("NhomID = 1");
            ddlTemp.Items.Clear();
            foreach (DataRow dr in drs)
            {
                ListItem liTemp = new ListItem();
                liTemp.Text = " " + dr["Tennhom"].ToString();
                liTemp.Value = dr["ID"].ToString();
                ddlTemp.Items.Add(liTemp);
                DataSet dsLevel = objNguoidung.loadNguoiDungLevel(intUserID);
                loadNhomIDSub(ddlTemp, dr["ID"].ToString(), dsQuyen.Tables[0], " ---");
            }
            ddlTemp.Visible = true;
        }
        else
        {
            ddlTemp.Visible = false;
        }
    }
    private void loadNhomPhu(DropDownList ddlTemp,int QuyenNhanID)
    {
        DataSet dsQuyen = objQuyen.loadGanQuyenByQuyenID(QuyenNhanID);
        if (dsQuyen != null && dsQuyen.Tables[0].Rows.Count > 0)
        {
            ddlTemp.Items.Clear();
            DataRow[] drs = dsQuyen.Tables[0].Select("NhomID = 1");
            if (drs.Length > 0)
            {
                foreach (DataRow dr in drs)
                {
                    ListItem liTemp = new ListItem();
                    liTemp.Text = " " + dr["Tennhom"].ToString();
                    liTemp.Value = dr["ID"].ToString();
                    ddlTemp.Items.Add(liTemp);
                    loadNhomIDSub(ddlTemp, dr["ID"].ToString(), dsQuyen.Tables[0], " ---");
                }
            }
            else
            {
                foreach (DataRow dr in dsQuyen.Tables[0].Rows)
                {
                    ListItem liTemp = new ListItem();
                    liTemp.Text = " " + dr["Tennhom"].ToString();
                    liTemp.Value = dr["ID"].ToString();
                    ddlTemp.Items.Add(liTemp);
                    loadNhomIDSub(ddlTemp, dr["ID"].ToString(), dsQuyen.Tables[0], " ---");
                }
            }            
            ddlTemp.Visible = true;
        }
        else
        {
            ddlTemp.Visible = false;
        }
    }
    private void loadNhomIDSub(DropDownList ddlTemp, string strCheck, DataTable dt, string strPath)
    {
        DataRow[] drs = dt.Select("NhomID =" + strCheck);
        foreach (DataRow dr in drs)
        {
            ListItem liTemp = new ListItem();
            liTemp.Text = strPath + dr["Tennhom"].ToString();
            liTemp.Value = dr["ID"].ToString();
            ddlTemp.Items.Add(liTemp);
            loadNhomIDSub(ddlTemp, dr["ID"].ToString(), dt, strPath + "---");
        }
    }
    private void InsertHistoryHocVien()
    { 
       
    }

#endregion
#region "Events handle"
    //Check quyen han su dung
    private void CheckUserModule(int UserID, int ModuleID)
    {
        
    }
    private void loadDataAccess()
    {
        SqlConnection sqlConnec = ConnectData.getConnection();
        objNguoidung = new DANguoidung(sqlConnec);
        objQuyen = new DAQuyen(sqlConnec);
        objHocVien = new DAHocVien(sqlConnec);
    }
    //Main load
    protected void Page_Load(object sender, EventArgs e)
    {
        // Put user code to initialize the page here
        intUserID = Convert.ToInt32(Session["UserID"].ToString().Trim());
        loadDataAccess();
        if (!Page.IsPostBack)
        {
            lbl_loi.Visible = false;
            loadNhomGoc(this.ddlNhomGoc);
            loadNhomPhu(this.ddlNhomPhu,Convert.ToInt32(this.ddlNhomGoc.SelectedValue));
            loadQuyenNguoiDung(Convert.ToInt32(this.ddlNhomGoc.SelectedValue),intUserID, this.tvNguoidungGoc, true);
            loadQuyenNguoiDung(Convert.ToInt32(this.ddlNhomPhu.SelectedValue),intUserID , this.tvNguoidungPhu, false);
        }
    }
    protected void ddlNhomPhu_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadQuyenNguoiDung(Convert.ToInt32(this.ddlNhomPhu.SelectedValue),intUserID, this.tvNguoidungPhu, false);
    }
    protected void ddlNhomGoc_SelectedIndexChanged(object sender, EventArgs e)
    {
        loadNhomPhu(this.ddlNhomPhu, Convert.ToInt32(this.ddlNhomGoc.SelectedValue));
        loadQuyenNguoiDung(Convert.ToInt32(this.ddlNhomGoc.SelectedValue),intUserID, this.tvNguoidungGoc, true);
        loadQuyenNguoiDung(Convert.ToInt32(this.ddlNhomPhu.SelectedValue),intUserID, this.tvNguoidungPhu, false);      
    }
    protected void uxLevelRightBtn_Click(object sender, ImageClickEventArgs e)
    {       
        string strHocVienIDs = "";
        int intNguoiBiChuyenID = 0;
        int intNguoiNhanID = 0;
        int intNguoiChuyenID = 0;
        int intLevelID = 0;
        if (tvNguoidungPhu.SelectedNode != null)
        {
            foreach (ComponentArt.Web.UI.TreeViewNode rootTemp in this.tvNguoidungPhu.Nodes)
            {
                if (rootTemp.IsSelected)
                {
                    intNguoiNhanID = Convert.ToInt32(rootTemp.Value.Trim());
                    break;
                }
            }
            foreach (ComponentArt.Web.UI.TreeViewNode rootTemp in this.tvNguoidungGoc.Nodes)
            {
                intNguoiBiChuyenID = Convert.ToInt32(rootTemp.Value.Trim());
                if (intNguoiBiChuyenID != intNguoiNhanID)
                {
                    if (rootTemp.Checked)
                    {
                        objHocVien.insertHistoryChuyenHocVien("", intNguoiChuyenID, intNguoiNhanID, intUserID, 0);
                        //Response.Write("<BR> Chon tat ca nguoi dung thu " + intNguoiBiChuyenID.ToString());
                    }
                    else
                    {
                        foreach (ComponentArt.Web.UI.TreeViewNode rootTemp1 in rootTemp.Nodes)
                        {
                            intLevelID = Convert.ToInt32(rootTemp1.Value.Trim());
                            if (rootTemp1.Checked)
                            {
                                objHocVien.insertHistoryChuyenHocVien("", intNguoiBiChuyenID, intNguoiNhanID, intUserID, intLevelID);
                                //Response.Write("<BR>Chon tat ca level thu " + intLevelID.ToString());
                                //insert tat ca hoc vien cua Nguoi dung nay theo Level
                            }
                            else
                            {
                                int i = 0;
                                strHocVienIDs = "";
                                foreach (ComponentArt.Web.UI.TreeViewNode rootTemp2 in rootTemp1.Nodes)
                                {
                                    if (rootTemp2.Checked)
                                    {
                                        strHocVienIDs += rootTemp2.Value.Trim() + ",";
                                        i++;
                                    }
                                }
                                if (i == rootTemp1.Nodes.Count)
                                {
                                    objHocVien.insertHistoryChuyenHocVien("", intNguoiBiChuyenID, intNguoiNhanID, intUserID, intLevelID);
                                }
                                else
                                {
                                    if (!strHocVienIDs.Equals(""))
                                    {
                                        objHocVien.insertHistoryChuyenHocVien(strHocVienIDs, intNguoiBiChuyenID, intNguoiNhanID, intUserID, intLevelID);
                                    }
                                }
                                //Response.Write("<BR>Chon tat contact thu " + strHocVienIDs.ToString()); 
                            }
                        }
                    }
                }
            }
            //Response.Write("<BR>Chon nguoi nhan " + intNguoiNhanID.ToString());
            loadQuyenNguoiDung(Convert.ToInt32(this.ddlNhomGoc.SelectedValue), intUserID, this.tvNguoidungGoc, true);
            loadQuyenNguoiDung(Convert.ToInt32(this.ddlNhomPhu.SelectedValue), intUserID, this.tvNguoidungPhu, false);
            lbl_loi.Visible = false;
        }
        else
        {
           lbl_loi.Visible=true;
        }
    }
#endregion
}
